//
//  CalendarView.m
//  FindJob
//
//  Created by apple on 16/3/9.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CalendarView.h"
#import "CalendarPageView.h"

@interface CalendarView ()<CalendarPageViewDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIView *weekTitleView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSDate *currentDate;
@property (nonatomic, strong) CalendarPageView *centerPageView;
@property (nonatomic, strong) CalendarPageView *leftPageView;
@property (nonatomic, strong) CalendarPageView *rightPageView;

@end
@implementation CalendarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:25.0/255 green:208.0/255 blue:222.0/255 alpha:1];
        self.currentDate = [NSDate date];
        [self addSubview:self.dateLabel];
        [self addSubview:self.weekTitleView];
        [self addSubview:self.scrollView];
        [self.scrollView addSubview:self.centerPageView];
        [self.scrollView addSubview:self.leftPageView];
        [self.scrollView addSubview:self.rightPageView];
    }
    return self;
}
#pragma mark - IScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat offsetX = scrollView.contentOffset.x;
    if (offsetX == scrollView.frame.size.width) {
        return;
    }else if (offsetX > scrollView.frame.size.width){
        self.currentDate = [self.centerPageView dateOfNextMonth];
        self.centerPageView.curretDisplayDate = self.currentDate;
        self.leftPageView.curretDisplayDate = [self.centerPageView dateOfLastMonth];
        self.rightPageView.curretDisplayDate = [self.centerPageView dateOfNextMonth];
    }else{
        self.currentDate = [self.centerPageView dateOfLastMonth];
        self.centerPageView.curretDisplayDate = self.currentDate;
        self.leftPageView.curretDisplayDate = [self.centerPageView dateOfLastMonth];
        self.rightPageView.curretDisplayDate = [self.centerPageView dateOfNextMonth];
    }
    [scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 1)];
    
    
}

#pragma mark - alendarPageViewDelegate
- (void)calendarPageView:(CalendarPageView *)pageView didSelectedDate:(NSDate *)date{
    
}
#pragma mark - private
- (NSString *)stringFromDate:(NSDate *)date{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM月yyyy"];
    NSString *dateStr = [formatter stringFromDate:date];
    return dateStr;
}
- (void)setupWeekTitles{
    NSArray *weekTitles =@ [@"周日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
    CGFloat lableWidth = self.weekTitleView.frame.size.width / weekTitles.count;
    for (int i = 0; i< weekTitles.count; i++) {
        CGRect frame = CGRectMake(i * lableWidth, 0, lableWidth, self.weekTitleView.frame.size.height);
        UILabel *lable = [[UILabel alloc] initWithFrame:frame];
        lable.font = [UIFont systemFontOfSize:14];
        lable.textColor = [UIColor whiteColor];
        lable.text = weekTitles[i];
        lable.textAlignment = NSTextAlignmentCenter;
        [self.weekTitleView addSubview:lable];
    }
}
#pragma mark - setters

- (void)setCurrentDate:(NSDate *)currentDate{
    _currentDate = currentDate;
    _dateLabel.text = [self stringFromDate:currentDate];
}
#pragma mark - getter
- (UILabel *)dateLabel{
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 30)];
        _dateLabel.textColor = [UIColor whiteColor];
        _dateLabel.textAlignment = NSTextAlignmentCenter;
        _dateLabel.font = [UIFont systemFontOfSize:17];
        
    }
    return _dateLabel;
}

- (UIView *)weekTitleView{
    if (!_weekTitleView) {
        _weekTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 30, self.frame.size.width, 25)];
        [self setupWeekTitles];
        
    }
    return _weekTitleView;
}
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 55, self.frame.size.width, self.frame.size.height - 55)];
        _scrollView.contentSize = CGSizeMake(3 * self.frame.size.width, self.scrollView.frame.size.height);
        _scrollView.contentOffset = CGPointMake(self.frame.size.width, 1);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.scrollEnabled= YES;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

- (CalendarPageView *)centerPageView{
    if (!_centerPageView) {
        CGSize size = self.scrollView.frame.size;
        
        _centerPageView = [[CalendarPageView alloc] initWithFrame:CGRectMake(size.width, 0, size.width, size.height) date:self.currentDate];
        _centerPageView.backgroundColor = [UIColor clearColor];
    }
    return _centerPageView;
}

- (CalendarPageView *)leftPageView{
    if (!_leftPageView) {
        _leftPageView = [[CalendarPageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) date:[self.centerPageView dateOfLastMonth]];
        _leftPageView.backgroundColor = [UIColor clearColor];
        _leftPageView.delegate = self;
    }
    return _leftPageView;
}

- (CalendarPageView *)rightPageView{
    if (!_rightPageView) {
        _rightPageView = [[CalendarPageView alloc] initWithFrame:CGRectMake(2 * self.scrollView.frame.size.width, 0, self.frame.size.width, self.frame.size.height) date:[self.centerPageView dateOfNextMonth]];
        _rightPageView.backgroundColor = [UIColor clearColor];
        _rightPageView.delegate = self;
    }
    return _rightPageView;
}






@end
